<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
</body>
<script>
;(function () {
    function Tools () {}

    Tools.prototype.post = function (url, callback, error) {
        var host = '/oms/auth'
        var ajax = new XMLHttpRequest()
        ajax.open('post', host + url, true)
        ajax.send()

        ajax.onreadystatechange = function () {
            if (ajax.readyState !== 4) return
            if (ajax.status == 200) {
                callback(ajax.response)
            } else {
                error(ajax.response)
            }
        }
    }

    Tools.prototype.getUrlParam = function (name) {
        var href = location.href

        if (href.indexOf('?') == -1) return

        var paramsString = href.split('?')[1],
            params = paramsString.split('&'),
            result = ''
        for (var i = 0; i < params.length; i++) {
            if (params[i].indexOf(name) > -1) {
                if (params[i].indexOf('=') == -1) return
                result = params[i].split('=')[1]
            }
        }

        return result
    }

    Tools.prototype.back = function () {
        window.history.back()
    }

    Tools.prototype.entry = function (account) {
        localStorage.setItem('account', JSON.stringify(account))
        sessionStorage.setItem('account', JSON.stringify(account))
        if( account.apps.length == 1 && (!account.apps[0].auProjectRoleRel || account.apps[0].auProjectRoleRel.length == 1 ) ){
            account.apps[0].status = 1;
            if( account.apps[0].auProjectRoleRel ){
                account.apps[0].auProjectRoleRel[0].status = "1";
                account.apps[0].auProjectRoleRel[0].selectStatus = 1;
            }
            window.location.href = "home.html";
        }else{
            window.location.href = "projects.html";
        }
    }




    var tools = new Tools()

    var username = tools.getUrlParam('username')
    tools.post('/autoLogin?username=' + username,
        function (res) {
            tools.entry(JSON.parse(res).data)
        }, 
        function (err) {
            console.log(err)
        }
    )



})()



</script>
</html>